<?php
/**
 * 此题无解 中转输入脚本 测试版
 *
 * @category   配置保存
 * @author      此题无解
 * 更多Web安全交流 Q群：260044099
 */

//检测来源，不安全
$p_url = str_replace($_SERVER['PHP_SELF'],'SaveConfig.php','');
$t_port = $_SERVER['SERVER_PORT'] == 80?'/':':'.$_SERVER['SERVER_PORT'].'/';
$r_url = 'http://'.$_SERVER['HTTP_HOST'].$t_port.$p_url;
$search = stripos(@$_SERVER['HTTP_REFERER'], $r_url);
if ($search === false) {
	header("HTTP/1.1 404 Not Found");
	die();
};


include("common.php");
//处理提交的数据
//print_r($_POST);

foreach ($_POST as $key => $value) {
    if (empty($value)) continue;
    $data[$key] = $value;
}

$result = CheckVaild($data, $REPLACETAG, $PackageChecked);
if ($result === True){
    echo json_encode(array('status'=>'ok'));

    $theme = isset($data['config'])?$data['config']:'default';
    //$file = '../config/'.$theme.'_tconfig.xml';
    $file = realpath('./config/').'/'.$theme.'_config.xml';
    WriteXML($file, $data);
}
else echo json_encode($result);

/*
$data_array = array(
    array(
    'title' => 'title1',
    'content' => 'content1',
        'pubdate' => '2009-10-11',
    ),
    array(
    'title' => 'title2',
    'content' => 'content2',
    'pubdate' => '2009-11-11',
    )
);
//  属性数组
$attribute_array = array(
    'title' => array(
    'size' => 1
    )
);
*/
function WriteXML($file="default",$data_array=null, $attribute_array=null){
	if (!is_array($data_array)) return "error param";
    $theme = 'default';
	$xml = new XMLWriter();
	$xml->openUri($file); //  输出方式，也可以设置为某个xml文件地址，直接输出成文件
	$xml->setIndentString('  ');
	$xml->setIndent(true);
	$xml->startDocument('1.0', 'utf-8');
	$xml->startElement('TransFormConfig');

    $xml->startElement($theme);
    if (is_array($data_array)) {
        foreach ($data_array as $key => $row) {
          	$xml->startElement($key);
          	if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
          	{
              foreach ($attribute_array[$key] as $akey => $aval) {
                    $xml->writeAttribute($akey, $aval);
                }
            }
            $xml->text($row);   //  设置内容
            $xml->endElement(); // $key
        }
    }
    $xml->endElement(); //  item

	$xml->endElement();
	$xml->endDocument();
	$xml->flush();
}
?>